package br.com.simora.crawler;

import javax.inject.Named;

import org.apache.log4j.Logger;

import br.com.simora.musica.Artista;
import br.com.simora.musica.Musica;
import br.com.simora.radio.RadioEnum;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

@Named("radiorock89fm")
public class RadioRock89FMCrawler extends AbstractCrawler {
	private static Logger logger = Logger.getLogger(RadioRock89FMCrawler.class);
	private static String URL = "http://player.radiorock.com.br/cron/integrador/results.json";

	@Override
	public void recuperaInfo() {
		String url = URL;
		recuperador.setUrl(url, "html");
		String artistaNome = "";
		String musicaNome = "";
		//logger.debug(recuperador.conteudo);
		try {
			
			String jsonConteudo = recuperador.getPagina().getConteudo();
			
			JsonParser jsonParser = new JsonParser();
			JsonObject jsonObject = (JsonObject) jsonParser.parse(jsonConteudo);
			
			JsonArray musicasElement= (JsonArray) jsonObject.get("musicas");
			JsonArray tocandoElement=null;
			if(musicasElement.size()>0){
				JsonObject objects = musicasElement.get(0).getAsJsonObject();
				tocandoElement= (JsonArray) objects.get("tocando");
				
				if(tocandoElement.size()>0){;
					JsonObject tocandoObj = tocandoElement.get(0).getAsJsonObject();
					artistaNome = tocandoObj.get("singer").getAsString();
					musicaNome = tocandoObj.get("song").getAsString();
				}
			}
			
			if(!"".equals(artistaNome)){
				artista = new Artista();
				artista.setNome(artistaNome.trim());
			}
			if(!"".equals(musicaNome)){
				musica = new Musica();
				musica.setNome(musicaNome.trim());
			}
			
			
		} catch (Exception e) {
			logger.debug(e.getMessage());
			e.printStackTrace();
		}
		logger.debug(artistaNome + " - " + musicaNome);
		
	}

	@Override
	public void setRadioNome() {
		this.radioNome = RadioEnum.radiorock89fm.getNome();
	}

}
